package com.cmc.cloud.cmclink.doc.service.impl.multimodaltransport;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cmc.cloud.cmclink.doc.api.document.emums.SoDataSourceEnum;
import com.cmc.cloud.cmclink.doc.mapper.ContainerManifestMapper;
import com.cmc.cloud.cmclink.doc.mapper.ManifestCntrMapper;
import com.cmc.cloud.cmclink.doc.mapper.ManifestMapper;
import com.cmc.cloud.cmclink.doc.mapper.SoForecastMapper;
import com.cmc.cloud.cmclink.doc.service.MultiManifestService;
import com.cmc.cloud.cmclink.doc.service.manifest.ManifestService;
import com.cmc.cloud.cmclink.doc.vo.manifestvo.manifestcntr.ManifestCntrSumVO;
import com.cmc.cloud.cmclink.doc.vo.multimodaltransport.multimanifest.MultiManifestReqVO;
import com.cmc.cloud.cmclink.doc.vo.multimodaltransport.multimanifest.MultiManifestRespVO;
import com.cmc.cloud.cmclink.doc.vo.sovo.soforecastvo.SoForecastCntrSumVO;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.security.core.util.SecurityFrameworkUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lzp
 * @date 2025/10/25 14:19
 * @description
 */
@Service
@Slf4j
public class MultiManifestServiceImpl implements MultiManifestService {

    @Resource
    private ManifestMapper manifestMapper;

    @Resource
    private SoForecastMapper soForecastMapper;

    @Resource
    private ContainerManifestMapper containerManifestMapper;

    @Resource
    private ManifestCntrMapper manifestCntrMapper;

    @Resource
    private ManifestService manifestService;

    @Override
    public PageResult<MultiManifestRespVO> searchMultiManifestInfo(MultiManifestReqVO multiManifestReqVo) {

        IPage<MultiManifestRespVO> iPage = Page.of(multiManifestReqVo.getPageNo(), multiManifestReqVo.getPageSize());
        IPage<MultiManifestRespVO> result = manifestMapper.searchMultiManifestInfo(iPage, multiManifestReqVo);
        List<MultiManifestRespVO> multiManifestRespVoList = result.getRecords();
        if (CollectionUtil.isEmpty(multiManifestRespVoList)) {
            return PageResult.empty();
        }
        List<String> bookingNos = multiManifestRespVoList.stream().map(MultiManifestRespVO::getBookingNo).collect(Collectors.toList());
        List<Long> manifestIds = multiManifestRespVoList.stream().map(MultiManifestRespVO::getId).collect(Collectors.toList());
        Map<Long, ManifestCntrSumVO> cntrMap = manifestCntrMapper.selectSumByManifestIds(manifestIds).stream().collect(Collectors.toMap(ManifestCntrSumVO::getManifestId, e -> e));
        Map<String, SoForecastCntrSumVO> forecastCntrMap = soForecastMapper.selectSumDescByBookingNo(bookingNos).stream().collect(Collectors.toMap(SoForecastCntrSumVO::getBookingNo, e -> e));

        for (MultiManifestRespVO item : multiManifestRespVoList) {
            SoForecastCntrSumVO soForecastCntrSumVo = forecastCntrMap.get(item.getBookingNo());
            if (null != soForecastCntrSumVo) {
                item.setForecast(soForecastCntrSumVo.getForecastCntrDesc());
            }
            ManifestCntrSumVO manifestCntrSumVo = cntrMap.get(item.getId());
            if (null != manifestCntrSumVo) {
                item.setContainer(manifestCntrSumVo.getCntrDesc());
            }
        }
        return new PageResult<>(multiManifestRespVoList, result.getTotal());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<Long> batchCreateMasterFromSo(List<Long> soIdList) {
        return manifestService.batchCreateMasterFromSo(soIdList, String.valueOf(SecurityFrameworkUtils.getLoginUserId()), SoDataSourceEnum.MANUAL.getValue(), true);
    }
}
